﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses
Imports Telerik.WebControls

Public Class ViewGalley
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim strSql As String
        'If LCase(Request.QueryString("type")) = "h" Then
        '    strSql = "select * from ptn_Gallery where Type='h' order by Id desc"
        'Else
        '    strSql = "select * from ptn_Gallery where Type='t' order by Id desc"
        'End If
        'Fill_Grid_Bind(gvGallery, strSql)
    End Sub

    Private Sub gvGallery_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvGallery.RowEditing
        Dim GId As Integer = gvGallery.Rows(e.NewEditIndex).Cells(0).Text
        Response.Redirect("EditGallery.aspx?gid=" & GId)
    End Sub

    'Protected Sub bindGridView(ByVal grd As Telerik.WebControls.RadGrid, Optional ByVal catId As String = Nothing)
    '    Dim strSql As String
    '    If catId Is Nothing Then
    '        strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_TourGallery tg on tg.GalleryId=g.Id inner join ptn_Tours t on t.Id=tg.TourId where g.Type='" & LCase(Request.QueryString("type")) & "' order by g.vieworder"
    '    Else
    '        strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_TourGallery tg on tg.GalleryId=g.Id inner join ptn_Tours t on t.Id=tg.TourId where g.Type='" & LCase(Request.QueryString("type")) & "' and t.categoryid=" & catId & " order by g.vieworder"
    '    End If
    '    Fill_Grid(grd, strSql)
    'End Sub

    Protected Sub bindGridView(ByVal grd As Telerik.WebControls.RadGrid, Optional ByVal catId As String = Nothing)
        Dim strSql As String
        If catId Is Nothing Then
            If LCase(Request.QueryString("type")) = "t" Then
                strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_TourGallery tg on tg.GalleryId=g.Id inner join ptn_Tours t on t.Id=tg.TourId where g.Type='" & LCase(Request.QueryString("type")) & "' order by g.vieworder"
            Else
                'strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_HotelGallery tg on tg.GalleryId=g.Id inner join ptn_Hotels t on t.Id=tg.HotelId where g.Type='" & LCase(Request.QueryString("type")) & "' order by g.vieworder"
                strSql = "select g.*, h.Name from ptn_Gallery g INNER JOIN ptn_HotelGallery hg ON g.id=hg.GalleryId INNER JOIN ptn_Hotels h ON h.id=hg.hotelId"
            End If
        Else
            If LCase(Request.QueryString("type")) = "t" Then
                strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_TourGallery tg on tg.GalleryId=g.Id inner join ptn_Tours t on t.Id=tg.TourId where g.Type='" & LCase(Request.QueryString("type")) & "' and t.categoryid=" & catId & " order by g.vieworder"
            Else
                strSql = "select g.*, t.Name from ptn_Gallery g inner join ptn_HotelGallery tg on tg.GalleryId=g.Id inner join ptn_Hotels t on t.Id=tg.HotelId where g.Type='" & LCase(Request.QueryString("type")) & "' and t.categoryid=" & catId & " order by g.vieworder"
            End If

        End If
        Fill_Grid(grd, strSql)
    End Sub

    Protected Sub radGrid_DeleteCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles radGrid.DeleteCommand
        Dim ID As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID").ToString
        Try
            'Dim txt As Array
            Dim sqlGal, sqlGalType As String
            sqlGal = "delete from ptn_gallery where Id=" & ID
            If LCase(Request.QueryString("type")) = "t" Then
                sqlGalType = "delete from ptn_tourgallery where GalleryId=" & ID
            Else
                sqlGalType = "delete from ptn_hotelgallery where GalleryId=" & ID
            End If
            Insert_Update_Delete_Record(sqlGal)
            Insert_Update_Delete_Record(sqlGalType)
            bindGridView(radGrid)
            'txt = RetriveData("select Thumbnail, Image from ptn_gallery where Id=" & ID, 1)
            'System.IO.File.Delete(Server.MapPath("~/images/img" & txt(0)))
            'System.IO.File.Delete(Server.MapPath("~/images/img" & txt(1)))
        Catch ex As Exception
            'radGrid.Controls.Add(New LiteralControl(("Unable to Delete Record. Reason: " + ex.Message)))
            lblError.Text = "Unable to Delete Record. Reason: " + ex.Message
            lblError.ForeColor = Drawing.Color.Red
            e.Canceled = True
        End Try
    End Sub

    Protected Sub radGrid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles radGrid.NeedDataSource
        bindGridView(radGrid)
        'Fill_Grid(radGrid, "select g.*, t.Name from Gallery g inner join TourGallery tg on tg.GalleryId=g.Id inner join Tours t on t.Id=tg.TourId where g.Type='" & LCase(Request.QueryString("type")) & "'")
        'radGrid.DataBind()

    End Sub
End Class